package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.acticle.query.AswerQueryObject;
import cn.wolfcode.luowowo.acticle.vo.AswerStatisVo;
import cn.wolfcode.luowowo.comment.domain.AnswerComment;
import cn.wolfcode.luowowo.comment.repository.IAnswerRepository;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class IAnswerCommentServiceImpl implements IAnswerCommentService {
    @Autowired
    private MongoTemplate mongoTemplate; //处理比较复杂的操作
    @Autowired
    private IAnswerRepository mongeRepository; //处理简单的crud
    //保存提问信息
    public void save(AnswerComment answerComment,String[] aswerTag) {
        AnswerComment comment = this.replay(answerComment);
        if(comment.getTitleId()==null){
            comment.setDestId(answerComment.getDestId());
            comment.setUserId(answerComment.getUserId());
            comment.setNickname(answerComment.getNickname());
            comment.setCity(answerComment.getCity());
            comment.setHeadImgUrl(answerComment.getHeadImgUrl());
            comment.setTitle(answerComment.getTitle());
            comment.setTop(answerComment.getTop());
            comment.setRemark(answerComment.getRemark());
            comment.setRead(answerComment.getRead());
            comment.setContent(answerComment.getContent());
            comment.setVserion(answerComment.getVserion());
            comment.setCoverUrl(answerComment.getCoverUrl());
        }
        List<String> list = new ArrayList<>();
        if(aswerTag!=null){
            for (String s : aswerTag) {
                if(s.length()>1){
                    list.add(s);
                }
            }
        }
        comment.setAswerTag(list);
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"titleId"));
        List<AnswerComment> comments = mongoTemplate.find(query, AnswerComment.class);
        if(answerComment.getVserion()==1){
            comment.setTitleId(comments.get(0).getTitleId()+1);
        }else {
            comment.setTitleId(answerComment.getTitleId());
        }
        if(comment.getUserId()!=null){
            SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String s = dateFormat.format(new Date());
            try {
                comment.setCrateTime(dateFormat.parse(s));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        mongeRepository.save(comment);
    }

    private AnswerComment replay(AnswerComment answerComment) {
        if(answerComment.getTitleId()!=null){
            Query query = new Query();
            query.addCriteria(Criteria.where("titleId").is(answerComment.getTitleId()));
            AnswerComment comment1 = mongoTemplate.findOne(query, AnswerComment.class);
            answerComment.setUserId(comment1.getUserId());
            answerComment.setTitle(comment1.getTitle());
            answerComment.setHeadImgUrl(comment1.getHeadImgUrl());
            answerComment.setNickname(comment1.getNickname());
            answerComment.setTop(comment1.getTop());
            answerComment.setCity(comment1.getCity());
            if(answerComment.getCoverUrl()!=null){
                answerComment.setCoverUrl(comment1.getCoverUrl());
            }else {
                answerComment.setCoverUrl(comment1.getCoverUrl());
            }
            answerComment.setDestId(comment1.getDestId());
            if(answerComment.getContent()!=null && answerComment.getContent().length()>=99){
                answerComment.setContext(answerComment.getContent().substring(0,99));

            }else {

                answerComment.setContent(answerComment.getContent());
            }
            answerComment.setVserion(answerComment.getVserion());
            return answerComment;
        }
        return new AnswerComment();

    }

    //查询出所有社区回答信息
    public List<AnswerComment> list( AswerQueryObject qo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("vserion").is(qo.getVserion()));
        Sort time = new Sort(Sort.Direction.DESC, "crateTime");
        query.with(time);
        List<AnswerComment> answerComments = mongoTemplate.find(query, AnswerComment.class);
        return answerComments;
    }
    //查询单个用户的回答问题
    public AnswerComment getByAnswerComment(Long id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(id));
        AnswerComment comment = mongoTemplate.findOne(query, AnswerComment.class);
        return comment;
    }
    //按照回答数据进行排序
    public List<AnswerComment> listSortRemark() {
        Query query = new Query();
        query.with(new Sort(Sort.Direction.DESC,"remark"));
        List<AnswerComment> answerComments = mongoTemplate.find(query, AnswerComment.class);
        return answerComments;
    }
    //根据标题id 查询回答信息
    public AnswerComment getByAnswerCommentTitle(Long titleId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("titleId").is(titleId));
        AnswerComment comment = mongoTemplate.findOne(query, AnswerComment.class);
        return comment;
    }
    //同步数据
    public void updateStatisData(AswerStatisVo vo) {
        mongoTemplate.save(vo);
    }

    public List<AnswerComment> getAnswerComment(Long titleId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("titleId").is(titleId));
        List<AnswerComment> comments = mongoTemplate.find(query, AnswerComment.class);
        return comments;
    }
    //查询所有数据

    public List<AnswerComment> getAll() {
        List<AnswerComment> all = mongeRepository.findAll();
        return all;
    }
}

